package cn.edu.zufe.mjt.manualarraylist;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author 蒙锦涛
 * @date 2025/5/15
 * @description 手动实现的动态数组类，用于存储和管理对象序列。支持动态扩容、元素增删等操作。
 */


public class ArrayList<E> implements List<E> {

    private Object[] table = new Object[10];
    private int size;

    @Override
    public void add(E element) {
        if (size == table.length) {
            resize();
        }
        table[size] = element;
        size++;
    }

    private void resize() {
        // 数组的扩容
        Object[] newTable = new Object[table.length * 2];
        System.arraycopy(table, 0, newTable, 0, table.length);
        this.table = newTable;
    }

    @Override
    public void add(E element, int index) {
        // 首先判断index是否合法
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        // 判断是否需要扩容
        if (size == table.length) {
            resize();
        }
        // 如果index小于size，则需要将index之后的元素向后移动一位
        System.arraycopy(table, index, table, index + 1, size - index);
        // 然后将element放入指定位置
        table[index] = element;
        size++;
    }

    @Override
    public E remove(int index) {
        // 判断index是否合法
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        @SuppressWarnings("unchecked")
        E removeElement = (E)table[index];
        System.arraycopy(table, index + 1, table, index, size - index - 1);
        size--;
        table[size] = null; // 置空最后一个元素，防止内存泄漏
        return removeElement;
    }

    @Override
    public boolean remove(E element) {
        for (int i = 0; i < size; i++) {
            if (element.equals(table[i])) {
                remove(i);
                return true;
            }
        }
        return false;
    }

    @Override
    public E set(int index, E element) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        @SuppressWarnings("unchecked")
        E oldElement = (E)table[index];
        table[index] = element;
        return oldElement;
    }

    @Override
    @SuppressWarnings("unchecked")
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
	    return (E)table[index];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<E> iterator() {
        return new ArrayListIterator();
    }


    /**
     *  这是一个迭代器的实现，用于遍历ArrayList中的元素。
     */
    class ArrayListIterator implements Iterator<E> {

        private int cursor;

        @Override
        public boolean hasNext() {
            return cursor != size;
        }

        @Override
        @SuppressWarnings("unchecked")
        public E next() {
            if (cursor >= size) {
                throw new NoSuchElementException();
            }
            E element = (E)table[cursor];
            cursor++;
            return element;
        }
    }

















}
